﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OrderApp;

namespace OrderAppWin
{
    public partial class OrderSystem : Form
    {
        OrderService orderService;
       // BindingSource fieldsBS = new BindingSource();
        public String Keyword { get; set; }
        public OrderSystem()
        {
            InitializeComponent();
            orderService = new OrderService();
            
            OrderItem Bread = new OrderItem(1, "面包", 10, 5);           
            Order order1= new Order(1, "Jimin", new List<OrderItem> ());
            order1.AddItem(Bread);
            orderService.AddOrder(order1);

            OrderItem Beef= new OrderItem(2, "牛肉", 50, 3);
            Order order2=new Order(2, "V", new List<OrderItem> ());
            order2.AddItem(Beef);
            orderService.AddOrder(order2);

            orderbindingSource.DataSource = orderService.Orders;
            orderView.DataSource = orderbindingSource;

            itembindingSource.DataSource = orderService.Orders;
            itemView.DataSource = itembindingSource;

            search_with.SelectedIndex = 0;
            search_text.DataBindings.Add("Text", this, "Keyword");

            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
           
        }

        private void Add_Click(object sender, EventArgs e)
        {

            OrderItems order_toadd = new OrderItems();

            if (order_toadd.ShowDialog() == DialogResult.OK)
            {
                orderService.AddOrder(order_toadd.CurrentOrder);
                QueryAll();
            }
            
        }

        private void Alter_Click(object sender, EventArgs e)
        {
            Alterorder();
        }

        private void QueryAll()
        {
            orderbindingSource.DataSource = orderService.Orders;
            orderbindingSource.ResetBindings(false);
        }
        private void Alterorder()
        {
            Order order = orderbindingSource.Current as Order;
            if(order == null)
            {
                MessageBox.Show("请选择要进行修改的订单");
                return;
            }
            OrderItems order_toalter = new OrderItems(order, true);
            if (order_toalter.ShowDialog() == DialogResult.OK)
            {
                orderService.UpdateOrder(order_toalter.CurrentOrder);
            }

        }

        private void Del_Click(object sender, EventArgs e)
        {
            Order order = orderbindingSource.Current as Order;
            if (order == null)
            {
                MessageBox.Show("请选择一个订单删除！");
                return;
            }
            orderService.RemoveOrder(order.OrderId);
            QueryAll();
        }

        private void Search_Click(object sender, EventArgs e)
        {
            switch (search_with.SelectedIndex)
            {
                case 0:
                    orderbindingSource.DataSource = orderService.Orders;
                    break;
                case 1:
                    int.TryParse(Keyword, out int id);
                    Order order = orderService.GetOrder((uint)id);

                    List<Order> result = new List<Order>();
                    if (order != null)
                        result.Add(order);
                    orderbindingSource.DataSource = result;
                    break;
                case 2:
                    orderbindingSource.DataSource = orderService.QueryOrdersByCustomerName(Keyword);
                    break;
                case 3:
                    orderbindingSource.DataSource = orderService.QueryOrdersByGoodsName(Keyword);
                    break;

            }
            orderbindingSource.ResetBindings(true);
        }

        private void search_with_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
